package com.eies.engine.jms;

import java.util.Hashtable;
import java.util.List;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

import com.eies.engine.common.ConfigUtil;
import com.eies.engine.process.Process;
import javax.naming.NamingException;
import java.util.ArrayList;

public class JMSSender {

	public void sendMsg(Process process) throws NamingException, JMSException {
		// init JNDI context
//		String JNDIFactory = "weblogic.jndi.WLInitialContextFactory";// define
		// JNDI
		// context
		// factory
//		String providerUrl = "t3://localhost:7001"; // define weblogic JMS url
		Hashtable env = new Hashtable();
		env.put(Context.INITIAL_CONTEXT_FACTORY, ConfigUtil.JNDIFactory);
		env.put(Context.PROVIDER_URL, ConfigUtil.providerUrl);
		Context ctx = new InitialContext(env);

		// find connection factory
		String connFactoryJNDI = ConfigUtil.connFactoryJNDI; // jms
		// String connFactoryJNDI = "ctais.jms.TopicConnectionFactory"; // jms
		// connectionFactory
		// JNDI name
		QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx
				.lookup(connFactoryJNDI);
		// create queue connection
		QueueConnection qConn = (QueueConnection) connFactory
				.createConnection();
		// create session
		QueueSession qSession = qConn.createQueueSession(false,
				Session.AUTO_ACKNOWLEDGE);
		// find queue by JNDI lookup
		Queue queue = (Queue) ctx.lookup(ConfigUtil.jmsFileQueue);
		// Queue queue = (Queue) ctx.lookup("ctais.jms.topic.dataSync");
		// create sender
		QueueSender qSender = qSession.createSender(queue);
		// create message
		ObjectMessage objectMessage = qSession.createObjectMessage(process);
		qSender.send(objectMessage);
		qSender.close();
		qSession.close();
		qConn.close();
	}

	// public static void main(String[] args) {
	// try {
	// P process = new P();
	// process.setList(new JMSSender().test());
	// new JMSSender().sendMsg(process);
	// } catch (NamingException e) {
	// e.printStackTrace();
	// } catch (JMSException e) {
	// e.printStackTrace();
	// }
	// }

	public List<String> test() {
		List<String> list = new ArrayList<String>();
		list.add("88888");
		return list;

	}
}
